#!/bin/bash 
#
# 安装完成后，会将一些账号密码信息，如数据库的，保存到 /root/account.txt 中
#

# 资源文件下载路径

kefuDownZip=http://down.zvo.cn/yunkefu/kefu-1.4.20220905.zip
kefuSql=https://gitee.com/leimingyun/yunkefu/raw/master/else/kefu.sql

#===============================================
# 加载常用资源下载URL，当然可能这些里面就 Propertids.jar 用到，但统一引用一个的，便于后续维护
cd ~
wget https://gitee.com/HuaweiCloudDeveloper/huaweicloud-solution-build-wangmarketcms/raw/master/shell/file.sh -O file.sh
chmod -R 777 file.sh
source ./file.sh
echo "加载资源文件下载路径完毕"


##### JDK、Tomcat、kefu等安装开始####
wget https://gitee.com/HuaweiCloudDeveloper/huaweicloud-solution-build-wangmarketcms/raw/master/shell/tomcat.sh -O tomcat.sh
chmod -R 777 file.sh
source ./tomcat.sh
echo "加载tomcat.sh完毕"
##### JDK、Tomcat安装结束####


#######Redis#####
echo '开始安装redis'
# 设置redis安装目录并进入
mkdir /mnt/redis/
cd /mnt/redis/

# 下载安装包
wget http://down.zvo.cn/centos/redis-5.0.3.zip

# 解压
unzip redis-5.0.3.zip

# 编译并将软件安装到指定的目录
make && make install PREFIX=/usr/local/redis

# 切换到服务所在目录
cd /usr/local/redis/bin/

# 配置自启动及后台运行服务
cp /mnt/redis/redis.conf /usr/local/redis/bin/

# 根据配置文件启动服务
nohup ./redis-server redis.conf >> /root/redis.log 2>&1 &

# 设置开机自启动
touch /etc/systemd/system/redis.service
echo '[Unit]'>>/etc/systemd/system/redis.service
echo 'Description=redis-server'>>/etc/systemd/system/redis.service
echo 'After=network.target'>>/etc/systemd/system/redis.service
echo '[Service]'>>/etc/systemd/system/redis.service
echo 'Type=forking'>>/etc/systemd/system/redis.service
echo 'ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/bin/redis.conf'>>/etc/systemd/system/redis.service
echo 'PrivateTmp=true'>>/etc/systemd/system/redis.service
echo '[Install]'>>/etc/systemd/system/redis.service
echo 'WantedBy=multi-user.target'>>/etc/systemd/system/redis.service
systemctl daemon-reload
systemctl start redis.service
systemctl enable redis.service

# 开机自启动
systemctl enable redis.service
echo 'redis安装完成'
sleep 3
#####end#########

#### 开始云客服应用包安装 ####
# 进入tomcat运行安装目录
cd /mnt/tomcat8/webapps/ROOT/
# 清空掉ROOT下的文件
rm -rf ../ROOT/*
# 下载最新客服应用
wget $kefuDownZip -O kefu.zip
# 解压
unzip kefu.zip
rm -rf kefu.zip
echo "kefu应用部署完毕"
#########end###########


######elasticsearch########

# 安装elasticsearch
echo '开始安装es'
cd ~

# 下载 ElasticSearch 7.10.1
wget https://mirrors.huaweicloud.com/elasticsearch/7.10.1/elasticsearch-7.10.1-linux-x86_64.tar.gz
tar -zxvf elasticsearch-7.10.1-linux-x86_64.tar.gz

# 创建elsearch用户组及elsearch用户

espassword=$RANDOM$RANDOM$RANDOM$RANDOM
groupadd elsearch
useradd elsearch -g elsearch -p $espassword

# 更改elasticsearch文件夹及内部文件的所属用户及组为elsearch:elsearch

chown -R elsearch:elsearch elasticsearch-7.10.1

# 将其转移到/mnt/elasticsearch
mkdir /mnt
mv elasticsearch-7.10.1 /mnt/elasticsearch

# 修改es配置文件
sed -i "s#^-Xms1g#-Xms128m#g" /mnt/elasticsearch/config/jvm.options
sed -i "s#^-Xmx1g#-Xmx128m#g" /mnt/elasticsearch/config/jvm.options

# 修改 elasticsearch 执行文件
sed -i '16i export JAVA_RELATIVE=/mnt/elasticsearch/jdk/ \
export JAVA_HOME=/mnt/elasticsearch/jdk/ \
export PATH=$JAVA_HOME/bin:$PATH \
echo JAVA_RELATIVE : $JAVA_RELATIVE \
#添加jdk判断 \
if [ -x "$JAVA_HOME/bin/java" ]; then \
        JAVA=$JAVA_RELATIVE \
else \
        JAVA=`which java` \
fi' /mnt/elasticsearch/bin/elasticsearch

# 修改 elasticsearch.yml 配置文件
sed -i '23s/#//g' /mnt/elasticsearch/config/elasticsearch.yml
sed -i '55s/#network.host: 192.168.0.1/network.host: 0.0.0.0/g' /mnt/elasticsearch/config/elasticsearch.yml
sed -i '72s/#//g' /mnt/elasticsearch/config/elasticsearch.yml
sed -i '72s/, "node-2"//g' /mnt/elasticsearch/config/elasticsearch.yml

# 新建 startup.sh 启动文件
touch /mnt/elasticsearch/startup.sh
echo 'ulimit -n 65536'>>/mnt/elasticsearch/startup.sh
echo 'sysctl -w vm.max_map_count=262144'>>/mnt/elasticsearch/startup.sh
echo 'su - elsearch -c /mnt/elasticsearch/bin/elasticsearch'>>/mnt/elasticsearch/startup.sh
echo 'echo "start up finish"'>>/mnt/elasticsearch/startup.sh

# 加入开机启动文件
echo '/mnt/elasticsearch/startup.sh'>>/etc/rc.d/rc.local

# 赋予可执行权限
chmod +x /mnt/elasticsearch/startup.sh
chmod -R 777 /mnt/elasticsearch/startup.sh
chmod +x /etc/rc.d/rc.local

## 启动
nohup /mnt/elasticsearch/startup.sh >> /root/es.log 2>&1 &
######elasticsearch end############



#### Mysql 安装后输出的root的登录密码，自己安装就用这种随机生成，要是资源编排安装那应该传入到~/install.properties 中，在去获取出来 ####
mysql_pwd=`openssl rand -base64 12`@wm
# 数据库名称
db_name=kefu
#############
# 安装mysql
wget https://gitee.com/HuaweiCloudDeveloper/huaweicloud-solution-build-wangmarketcms/raw/master/shell/mysql.sh -O mysql.sh
chmod -R 777 mysql.sh
source ./mysql.sh
echo "加载mysql.sh完毕"


############# 创建yunkefu数据库并导入sql文件开始 #################
# Mysql安装的一些变量设置等。wangmarket数据库是特定的，只有mysql安装在本身服务器才有的
# 创建 kefu 数据库
mysql -uroot -p$mysql_pwd -e "create database IF NOT EXISTS ${db_name}"
# 导入sql
cd /root
wget $kefuSql -O kefu.sql
mysql -uroot -p$mysql_pwd $db_name -e "source /root/kefu.sql"
sleep 3
############# 导入sql文件结束 #################


################ 注意，下面这些是当前特有的 ##################
#
# 自动修改wangmarket中的system数据表中的数据
#
# 配置安装域名相关-cdn附件域名，获取的格式如 kefu0001.xxxxxx.com
cdndomain=`java -cp ~/properties.jar Properties -path=/root/install.properties -get cdndomain`
mysql -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='https://$cdndomain/' WHERE name = 'ATTACHMENT_FILE_URL'"
# 配置存储方式为华为云OBS存储
mysql -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='huaWeiYunOBS' WHERE name = 'ATTACHMENT_FILE_MODE'"
##### 配置华为云的一些参数 #####
# 配置华为云AK
# 下载DSA加密支持
cd ~
wget http://down.zvo.cn/wm/version/wm-2.20.jar -O wm.jar
wget http://down.zvo.cn/jar/xnx3-util-1.9.jar -O xnx3-util.jar
######## 获取华为云AK进行加密后加入配置 #######
install_accesskeyid_original=`java -cp ~/properties.jar Properties -path=/root/install.properties -get accesskey.id`
install_accesskeysecret_original=`java -cp ~/properties.jar Properties -path=/root/install.properties -get accesskey.secret`
# 加密
install_accesskeyid=`java -cp ./wm.jar:./xnx3-util.jar  com.xnx3.j2ee.util.DSAUtil ${install_accesskeyid_original}`
install_accesskeysecret=`java -cp ./wm.jar:./xnx3-util.jar  com.xnx3.j2ee.util.DSAUtil ${install_accesskeysecret_original}`
# 将AK保存到配置中
mysql -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='$install_accesskeyid' WHERE name = 'HUAWEIYUN_ACCESSKEYID'"
mysql -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_ACCESSKEYID', '$install_accesskeyid')"
mysql -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='$install_accesskeysecret' WHERE name = 'HUAWEIYUN_ACCESSKEYSECRET'"
mysql -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_ACCESSKEYSECRET', '$install_accesskeysecret')"
# 配置华为云OBS - bucketname
install_obsbucket=`java -cp ~/properties.jar Properties -path=/root/install.properties -get obs.bucket`
mysql -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='$install_obsbucket' WHERE name = 'HUAWEIYUN_OBS_BUCKETNAME'"
mysql -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_OBS_BUCKETNAME', '$install_obsbucket')"
# 配置华为云可用区，如 ap-southeast-1
install_region=`java -cp ~/properties.jar Properties -path=/root/install.properties -get region`
mysql -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='$install_region' WHERE name = 'HUAWEIYUN_COMMON_ENDPOINT'"
mysql -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_COMMON_ENDPOINT', '$install_region')"
# 配置华为云OBS - endpoint ，格式如 obs.ap-southeast-1.myhuaweicloud.com
mysql -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='obs.$install_region.myhuaweicloud.com' WHERE name = 'HUAWEIYUN_OBS_ENDPOINT'"
mysql -uroot -p$mysql_pwd $db_name -e "INSERT INTO system(name,value) VALUES('HUAWEIYUN_OBS_ENDPOINT', 'obs.$install_region.myhuaweicloud.com')"
# 禁止通过 /install/index.do 在安装系统
mysql -uroot -p$mysql_pwd $db_name -e "UPDATE system SET value='false' WHERE name = 'IW_AUTO_INSTALL_USE'"


#
# 修改配置文件中的一些配置
#
# 开启elasticsearch
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set wm.elasticsearch.hostname=127.0.0.1
# 开启redis
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set spring.redis.host=127.0.0.1
# 连接的账密
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set database.name=kefu
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set database.ip=127.0.0.1
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set spring.datasource.username=root
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties -set spring.datasource.password=$mysql_pwd
# 修改数据库连接为mysql方式，而非默认的sqlite
sed -i "s#^spring.jpa.database-platform=.*# #g" /mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties
sed -i "s#^spring.datasource.driver-class-name=.*#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#g" /mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties
sed -i "s#^spring.datasource.url=.*#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/kefu\?useUnicode=true\&characterEncoding=utf-8#g" /mnt/tomcat8/webapps/ROOT/WEB-INF/classes/application.properties

# 修改kefu.properties
ecsip=`java -cp ~/properties.jar Properties -path=/root/install.properties -get ecs.ip`
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/kefu.properties -set kefu.api=http://$ecsip
java -cp ~/properties.jar Properties -path=/mnt/tomcat8/webapps/ROOT/WEB-INF/classes/kefu.properties -set kefu.socket=ws://$ecsip:8081

# 输出mysql配置信息到/root/account.txt
ecspassword=`java -cp ~/properties.jar Properties -path=/root/install.properties -get ecs.password`
# 配置将文件地址
txt_path=/root/account.txt
touch $txt_path
echo '======= mysql5.7 ======' >> $txt_path
echo '数据库名称:'$db_name >> $txt_path
echo '登录用户名:root' >> $txt_path
echo '登录的密码:'$mysql_pwd >> $txt_path
echo ' ' >> $txt_path
echo '======= 服务器 ======' >> $txt_path
echo '服务器IP:'$ecsip >> $txt_path
echo '登录用户名:root' >> $txt_path
echo '登录的密码:'$ecspassword >> $txt_path
echo ' ' >> $txt_path
echo '======= 对象存储 OBS ======' >> $txt_path
echo '可用区:'$install_region >> $txt_path
echo '桶名字:'$install_obsbucket >> $txt_path


# 删除 file.sh 、mysql.sh 等通用模块
rm -rf /root/file.sh
rm -rf /root/mysql.sh
rm -rf /root/tomcat.sh

systemctl stop firewalld.service
# 启动tomcat
echo "启动tomcat"
/mnt/tomcat8/bin/startup.sh
#看启动日志
#echo "看启动日志"
echo "自动部署完成，您可以正常使用了！打开浏览器，访问ip即可使用"